package com.monitor.zk.zookeeper.core.schedule;

import bean.ProjectInfo;
import com.monitor.zk.zookeeper.command.*;
import core.Request;
import com.monitor.zk.zookeeper.core.RequestEnum;
import com.monitor.zk.zookeeper.handle.PrepareHandleProcess;
import com.monitor.zk.zookeeper.handle.RUN_STATUS;
import com.monitor.zk.zookeeper.handle.SendThreadHandleProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
@Slf4j
public class MonitorSchedule {

    private final LinuxProjectInfoCommand linuxProjectInfoCommand;
    private final LinuxSystemInfoCommand linuxSystemInfoCommand;
    private final VMInfoCommand vmInfoCommand;

    private static PrepareHandleProcess prepareHandleProcess;
    private static SendThreadHandleProcess sendThreadHandleProcess;

    public MonitorSchedule(){
        linuxProjectInfoCommand = ExecCommandFactory.getLinuxProjectInfoCommand();
        linuxSystemInfoCommand = ExecCommandFactory.getLinuxSystemInfoCommand();
        vmInfoCommand = ExecCommandFactory.getVmInfoCommand();
        sendThreadHandleProcess = new SendThreadHandleProcess();
        prepareHandleProcess = new PrepareHandleProcess(sendThreadHandleProcess);

        prepareHandleProcess.start();
        sendThreadHandleProcess.start();
    }

    public static void shutdown(){
        if(prepareHandleProcess.STATUS == RUN_STATUS.RUN){
            prepareHandleProcess.STATUS = RUN_STATUS.STOP;
            prepareHandleProcess.interrupt();
        }
        if(sendThreadHandleProcess.STATUS == RUN_STATUS.RUN){
            sendThreadHandleProcess.STATUS = RUN_STATUS.STOP;
            sendThreadHandleProcess.interrupt();
        }
    }

    @Scheduled(cron = "*/5 * * * * *")
    public void get5sVMData(){
        Request request = new Request();
        request.setRequestType(RequestEnum.VMINFO);
        request.setBody(vmInfoCommand.vmMemory().toJson());
        prepareHandleProcess.exec(request);
    }

    @Scheduled(cron = "*/5 * * * * *")
    public void get5sSYSData(){
        try {
            Request request = new Request();
            request.setBody(linuxSystemInfoCommand.getAllMonitor().toJson());
            request.setRequestType(RequestEnum.LINUXINFP);
            prepareHandleProcess.exec(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "*/5 * * * * *")
    public void get5sPROJData(){
        try {
            List<ProjectInfo> infoList = linuxProjectInfoCommand.getProjectMonitor();
            infoList.forEach(projectInfo -> {
                Request request = new Request();
                request.setRequestType(RequestEnum.PROJECTINFO);
                request.setBody(projectInfo.toJson());
                prepareHandleProcess.exec(request);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
